// ignore_for_file: no_logic_in_create_state

import 'package:flutter/material.dart';

import '../../../components/Search/Search.dart';
import '../../../store/project/project.dart';
import '../../content-layout.dart';
import 'ProjectForm.dart';

class ProjectView extends StatefulWidget {
  const ProjectView({super.key});

  @override
  State<ProjectView> createState() => _ProjectViewState();
}

class _ProjectViewState extends State<ProjectView> {
  List<dynamic> projectList = [];

  Map<String, String>? selectedData;

  loadList() async {
    dynamic list = await ProjectStore.getProjectList();
    setState(() {
      projectList = list;
    });
  }

  @override
  void initState() {
    super.initState();
    loadList();
  }

  Widget get renderHeader {
    return const Row(
      children: [
        SearchWidget(),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return ContentLayout(
      leftSlot: ProjectList(list: projectList),
      rightSlot: ProjectForm(
        onConfirm: () {
          loadList();
        },
      ),
      headerSlot: renderHeader,
    );
    // return Scaffold(
    //     appBar: AppBar(
    //         actions: [
    //           Builder(
    //             builder: (context) {
    //               return ElevatedButton(
    //                 child: const Text('新建项目'),
    //                 onPressed: () {
    //                   Scaffold.of(context).openDrawer();
    //                 },
    //               );
    //             },
    //           ),
    //           ElevatedButton(
    //             onPressed: () {
    //               print('clear');
    //             },
    //             child: const Text('新建项目'),
    //           ),
    //         ],
    //         title: const Text(
    //           '我的项目',
    //           style: TextStyle(fontWeight: FontWeight.bold),
    //         )),
    //     drawer: renderDrawer,
    //     body: Column(
    //       children: [Expanded(child: renderContent)],
    //     ));
  }

  Drawer get renderDrawer {
    return Drawer(
        width: 400,
        child: Builder(builder: (context) {
          return ProjectForm(
            formData: selectedData,
            onConfirm: () {
              Scaffold.of(context).closeDrawer();
              loadList();
            },
          );
        }));
  }
}

class ProjectList extends StatelessWidget {
  final List<dynamic> list;
  const ProjectList({super.key, required this.list});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      // padding: const EdgeInsets.all(8),
      itemCount: list.length,
      itemBuilder: (context, index) => Container(
          height: 80,
          width: double.infinity,
          margin: const EdgeInsets.only(bottom: 8),
          decoration: const BoxDecoration(
            color: Colors.white,
          ),
          child: Padding(
            padding: const EdgeInsets.all(10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  list[index]["name"],
                  style: const TextStyle(
                      fontWeight: FontWeight.w600,
                      fontSize: 18,
                      color: Color.fromRGBO(95, 107, 158, 1)),
                ),
                Text(
                  list[index]["path"],
                  style: const TextStyle(
                      fontWeight: FontWeight.w600,
                      fontSize: 16,
                      overflow: TextOverflow.ellipsis,
                      color: Color.fromRGBO(200, 200, 200, 1)),
                )
                // Expanded(child: Text('${list[index]["desc"]}'),)
              ],
            ),
          )),
    );
  }
}
